#!/usr/bin/env python
# _*_ coding: utf-8 _*_
# @Time : 2020/7/15 17:04
# @Author : LZQ
# @Version：V 0.1
# @File : rights.py
# @desc : 所以权限相关的操作
from . import api, right
from __init__ import db, redis_store
from app.utils.status_code import RET
from flask import current_app, g, session, request, jsonify
from sqlalchemy.exc import IntegrityError
from sqlalchemy import or_, and_, desc
from app.models import Permission, Permissionapi, Role
import json, time
from app.token_verify import verify


@right.route("/tree", methods=['GET'])
@verify
def get_permission_tree():
    permission_list = []
    try:
        permissions = Permission.query.filter(Permission.ps_level == '0').all()
        for permission in permissions:
            dict_level1 = {}
            dict_level1['id'] = permission.ps_id
            dict_level1['authName'] = permission.ps_name
            dict_level1['level'] = permission.ps_level
            dict_level1['path'] = permission.path[0].ps_api_path
            dict_level1['children'] = []
            level2s = Permission.query.filter(Permission.ps_pid == permission.ps_id,
                                              and_(Permission.ps_level == '1')).all()
            # 二级权限
            for level2 in level2s:
                dict_level2 = {}
                dict_level2['id'] = level2.ps_id
                dict_level2['authName'] = level2.ps_name
                dict_level2['level'] = level2.ps_level
                dict_level2['path'] = level2.path[0].ps_api_path
                dict_level2['children'] = []
                level3s = Permission.query.filter(Permission.ps_pid == level2.ps_id,
                                                 and_(Permission.ps_level == '2')).all()
                # 三级权限
                for level3 in level3s:
                    dict = {}
                    dict['id'] = level3.ps_id
                    dict['authName'] = level3.ps_name
                    dict['level'] = level3.ps_level
                    dict['path'] = level3.path[0].ps_api_path
                    dict_level2['children'].append(dict)
                dict_level1['children'].append(dict_level2)
            permission_list.append(dict_level1)
        data = {"data": permission_list, "meta": RET.PERMISSION_GET_LIST_SUCCESS}
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(RET.PERMISSION_GET_LIST_FAIL)
    return jsonify(data)

@right.route("/list", methods=['GET'])
@verify
def get_permission_list():
    permission_list = []
    try:
        permissions = Permission.query.filter().all()
        for permission in permissions:
            dict_level1 = {}
            dict_level1['id'] = permission.ps_id
            dict_level1['authName'] = permission.ps_name
            dict_level1['level'] = permission.ps_level
            dict_level1['path'] = permission.path[0].ps_api_path
            permission_list.append(dict_level1)
        data = {"data": permission_list, "meta": RET.PERMISSION_GET_LIST_SUCCESS}
    except Exception as e:
        current_app.logger.error(e)
        return jsonify(RET.PERMISSION_GET_LIST_FAIL)
    return jsonify(data)
